package com.adcc.soundalarm.fx.app;

import com.adcc.soundalarm.config.system.AlarmFactory;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import java.awt.*;

/**
 * Created by ZHANG on 2017/3/23.
 */
public class SysAlarmView {
    
    private SysAlarmViewController sysAlarmViewController;

    private Stage stageSysAlarm;

    private FXMLLoader loaderAlarm;

    public void start() throws Exception {
        if(loaderAlarm == null){
            loaderAlarm = new FXMLLoader(getClass().getResource("sys_alarm_view.fxml"));
        }
        if(stageSysAlarm == null){
            stageSysAlarm = new Stage(StageStyle.UNDECORATED);
            Parent root = loaderAlarm.load();
            sysAlarmViewController = loaderAlarm.getController();
            sysAlarmViewController.buildEnvironment(stageSysAlarm);
            stageSysAlarm.setTitle("系统告警");
            stageSysAlarm.setScene(new Scene(root));
            stageSysAlarm.initStyle(StageStyle.UTILITY);
            stageSysAlarm.setResizable(false);
            stageSysAlarm.setOnCloseRequest(new EventHandler<WindowEvent>(){
                @Override
                public void handle(WindowEvent event) {
                    event.consume();
                    stageSysAlarm.close();
                }
            });
            sysAlarmViewController.init();
            location(stageSysAlarm);
            stageSysAlarm.show();
        }else {
            if(!stageSysAlarm.isShowing()){
                stageSysAlarm.show();
            }
        }
    }


    private void location(Stage stage){
        Dimension dimenson = Toolkit.getDefaultToolkit().getScreenSize();
        stage.setWidth(955);
        stage.setHeight(299);
        double x = (dimenson.width - stage.getWidth())/2 ;
        double y = (dimenson.height - stage.getHeight())/2;
        if(dimenson.width - x - stage.getWidth() < 510){
            x = dimenson.width - 510 - stage.getWidth();
        }
        stage.setX(x);
        stage.setY(y);
    }
}
